package com.notetool.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

public class NoteSet
{

    ////////////////////
    // Members
    private TreeSet<Note> notes;
    
    ////////////////////
    // Constructors
    public NoteSet ()
    {
        notes = new TreeSet<Note> ();
    }
    
    public NoteSet (TreeSet<Note> baseSet)
    {
        // Clone set passed as a parameter
        if (baseSet != null)
            notes = new TreeSet<Note> (baseSet);
        else
            notes = new TreeSet<Note> ();
    }
    
    public NoteSet (NoteSet baseSet)
    {
        if (baseSet != null)
            notes = new TreeSet<Note> (baseSet.getNotes ());
        else
            notes = new TreeSet<Note> ();
    }
    
    ////////////////////
    // Accessors
    private TreeSet<Note> getNotes ()
    {
        return notes;
    }
    
    ////////////////////
    // Public methods
    public void addNote (Note note)
    {
        // Add note
        notes.add (note);
    }
    
    public void removeNote (Note note)
    {
        // Remove note
        notes.remove(note);
    }
    
    public boolean isEmpty ()
    {
        return notes.isEmpty ();
    }
    
    public Iterator<Note> getIterator ()
    {
        return notes.iterator();
    }
    
    public Note[] toArray ()
    {
        return notes.toArray (new Note[0]);
    }
    
    public ArrayList<Note> toArrayList ()
    {
        ArrayList<Note> list = new ArrayList<Note> ();
        
        Iterator<Note> iterator = notes.iterator ();
        while (iterator.hasNext ())
        {
            list.add (iterator.next ());
        }
        
        return list;
    }
    
    public void intersect (NoteSet set)
    {
        // Do intersect
        notes.retainAll (set.getNotes ());
    }
    
    public static NoteSet intersect (NoteSet set1, NoteSet set2)
    {
        // Clone the first set
        NoteSet clonedSet = new NoteSet (set1);
        
        // Do actual intersect
        clonedSet.intersect (set2);
        
        return clonedSet;
    }
    
    public void union (NoteSet set)
    {
        // Do intersect
        notes.addAll (set.getNotes ());
    }
    
    public static NoteSet union (NoteSet set1, NoteSet set2)
    {
        // Clone the first set
        NoteSet clonedSet = new NoteSet (set1);
        
        // Do actual intersect
        clonedSet.union (set2);
        
        return clonedSet;
    }
    
    ////////////////////
    // Private methods
    
}
